/*
 * UserFacade.java
 *
 * Created on 7 de Julho de 2007, 11:29
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.com.ecs.persistence.facade;

import br.com.ecs.persistence.entity.SystemUser;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Danielle
 */
@Stateless
public class UserFacade implements UserFacadeLocal {

    @PersistenceContext
    private EntityManager em;
    
    /** Creates a new instance of UserFacade */
    public UserFacade() {
    }

    public void create(SystemUser user) {
        em.persist(user);
    }

    public void edit(SystemUser user) {
        em.merge(user);
    }

    public void destroy(SystemUser user) {
        em.merge(user);
        em.remove(user);
    }

    public SystemUser find(Object pk) {
        return (SystemUser) em.find(SystemUser.class, pk);
    }
    
    public SystemUser findByName(String username) {
        Query findUser = em.createNamedQuery("SystemUser.findByName");  
        findUser.setParameter("username",username);
        return (SystemUser) findUser.getSingleResult();
               
    }
    
     public SystemUser findSystemUser(String login,String password) {
        Query findUser = em.createNamedQuery("SystemUser.findSystemUser");  
        findUser.setParameter("login",login);
        findUser.setParameter("password",password);
        return (SystemUser) findUser.getSingleResult();
               
    }
    
    public List findAll() {
        return em.createQuery("select object(o) from SystemUser as o").getResultList();
    }
    
}
